package com.group.project.hrms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.group.project.hrms.entity.ContactBasic;
import com.group.project.hrms.entity.ResultEntity.ResultEntity;
import com.group.project.hrms.entity.SystemAuthenticationList;
import com.group.project.hrms.service.IContactBasicService;
import com.group.project.hrms.service.ISystemAuthenticationListService;
import com.group.project.hrms.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class SystemAuthenticationListController {

    @Autowired
    private ISystemAuthenticationListService iSystemAuthenticationListService;

    @Autowired
    private IContactBasicService iContactBasicService;

    @Autowired
    private LogUtil logUtil;


    /**
     * 接收前端请求，获取权限清单并返回给前端页面
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/insertOrUpdateAuthenticationList")
    @ResponseBody
    public ResultEntity insertOrUpdateAuthenticationList(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");
        //System.out.println(map); //{contactID=d409897c2f, authID=[auth0001]}
        if(null != map){
            Object contactID = map.get("contactID");
            Object authID = map.get("authID");
            if(null != contactID && null != authID){
                List<String> authIDList = new ArrayList<>();
                JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(authID));
                for (int i = 0; i < jsonArray.size(); i++) {
                    authIDList.add(jsonArray.getString(i));
                }
                try{
                    SystemAuthenticationList authenticationList = iSystemAuthenticationListService.selectSystemAuthenticationListByContactId(contactID.toString());
                    if(null != authenticationList){
                        //更新操作
                        authenticationList.setmTime(date);
                        authenticationList.setAuthIds(JSON.toJSONString(authIDList));
                        iSystemAuthenticationListService.updateSystemAuthenticationList(authenticationList);
                    }else {
                        //插入操作
                        authenticationList = new SystemAuthenticationList();
                        authenticationList.setcTime(date);
                        authenticationList.setContactId(contactID.toString());
                        authenticationList.setAuthIds(JSON.toJSONString(authIDList));
                        iSystemAuthenticationListService.insertSystemAuthenticationList(authenticationList);
                    }
                    ContactBasic contactBasic = iContactBasicService.selectContactBasicByContactID(contactID.toString());
                    logUtil.insertSystemEventLog(staffName.toString(), date, "更新了" + contactBasic.getStaffName() + "的权限", "系统");
                    resultEntity.setCode("S");
                    resultEntity.setMsg("Success");
                    return resultEntity;
                }catch (Exception e){
                    e.printStackTrace();
                    resultEntity.setCode("F");
                    resultEntity.setMsg("操作失败");
                    return resultEntity;
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("传入参数错误");
                return resultEntity;
            }
        }else {
            resultEntity.setCode("F");
            resultEntity.setMsg("传入参数错误");
            return resultEntity;
        }
    }


    /**
     * 接收前端请求，删除用户权限
     * @param map 需要删除的用户信息
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/deleteAuthenticationList")
    @ResponseBody
    public ResultEntity deleteAuthenticationList(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");
        //System.out.println(map);
        if(null != map){
            Object contactId = map.get("contactId");
            if(null != contactId){
                SystemAuthenticationList authenticationList = iSystemAuthenticationListService.selectSystemAuthenticationListByContactId(contactId.toString());
                iSystemAuthenticationListService.deleteSystemAuthenticationList(authenticationList);
                ContactBasic contactBasic = iContactBasicService.selectContactBasicByContactID(contactId.toString());
                logUtil.insertSystemEventLog(staffName.toString(), date, "删除了" + contactBasic.getStaffName() + "的权限", "系统");
                resultEntity.setCode("S");
                resultEntity.setMsg("Success");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("删除失败");
        return resultEntity;
    }
}
